
package com.pp.admin.common.client;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class PermissionNode implements Serializable {
	
	/**
	 * 
	 */
	private static final long		serialVersionUID	= 6066056300498798128L;
	
	private String					permission			= "";
	
	private List<PermissionNode>	childs				= new ArrayList<PermissionNode>();
	
	public PermissionNode() {

	}
	
	public PermissionNode( String permission ) {

		this.permission = permission;
	}
	
	public void setPermission( String permission ) {

		this.permission = permission;
	}
	
	public List<PermissionNode> getChilds() {

		return childs;
	}
	
	public boolean checkPermission( String permissionStr ) {

		boolean resp = false;
		if ( permissionStr != null && permissionStr.contains( "." ) ) {
			String token = permissionStr.substring( 0, permissionStr.indexOf( '.' ) );
			if ( token.equalsIgnoreCase( permission ) ) {
				Iterator<PermissionNode> itrPermissins = childs.iterator();
				PermissionNode permissionNode = null;
				while ( itrPermissins.hasNext() ) {
					permissionNode = itrPermissins.next();
					resp = permissionNode.checkPermission( permissionStr.substring( permissionStr.indexOf( '.' ) ) );
					if ( resp ) {
						break;
					}
				}
			}
		} else if ( permissionStr != null ) {
			resp = permissionStr.equalsIgnoreCase( permission );
		}
		return resp;
	}
}
